<?php

class Common_Chart_Strategy_Google_Bar_HorizontalDynamic extends Common_Chart_Strategy_Google_Bar_HorizontalGrouped {

    const DEFAULT_BAR_SIZE = 15;
    const DEFAULT_BAR_SPACING = 6;
    const DEFAULT_GROUP_SPACING = 6;

    public function __construct() {
        parent::__construct();
    }

    public function get_chart_type() {
        return Common_Chart::TYPE_HORIZONTAL_BAR_DYNAMIC;
    }

    protected function apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options = array()) {

        $arr_chart_parameters = parent::apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options);
        unset($arr_chart_parameters[self::PARAMETER_CHART_LABELS]);

        return $arr_chart_parameters;
    }

    protected function apply_bar_options($arr_chart_parameters, $arr_data, $arr_options = array()) {

        if(!isset($arr_options['bar_size'])) {
            $arr_options['bar_size'] = self::DEFAULT_BAR_SIZE;
        }

        if(!isset($arr_options['spacing_between_bars'])) {
            $arr_options['spacing_between_bars'] = self::DEFAULT_BAR_SPACING;
        }

        if(!isset($arr_options['spacing_between_groups'])) {
            $arr_options['spacing_between_groups'] = self::DEFAULT_GROUP_SPACING;
        }

        $str_bar_size       = $arr_options['bar_size'];
        $flt_bar_spacing    = $arr_options['spacing_between_bars'];
        $flt_group_spacing  = $arr_options['spacing_between_groups'];

        $int_chart_height = isset($arr_options['height']) ? $arr_options['height'] : 0;
        if(is_numeric($str_bar_size)) {

            $int_group_items = count($arr_data);
            $int_data_items = count(end($arr_data));

            $int_height = 30 + ($str_bar_size * $int_data_items * $int_group_items) + ($flt_bar_spacing * $int_data_items) + ($flt_group_spacing * $int_group_items);

            if($int_height > $int_chart_height) {

                $this->set_dynamic_height($int_height);

            } else {
                unset($arr_options['bar_size']);
                unset($arr_options['spacing_between_bars']);
                unset($arr_options['spacing_between_groups']);
            }

        }

        $arr_chart_parameters = parent::apply_bar_options($arr_chart_parameters, $arr_data, $arr_options);


        return $arr_chart_parameters;
    }

}